package com.guigu.buy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.buy.pojo.Customer;
import com.guigu.buy.pojo.Merchant;
import com.github.pagehelper.PageInfo;
import com.guigu.buy.dto.MerchantUtil;
import com.guigu.buy.service.ICustomerService;
import com.guigu.buy.service.IMerchantService;
import com.guigu.buy.utils.PageUtils;
import com.guigu.buy.vo.MerchantVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author com.guigu
 * @since 2024-05-15
 */
@CrossOrigin
@Controller
@RequestMapping("/buy/merchant")
public class MerchantController {
    @Autowired
    private IMerchantService merchantService;
    @Autowired
    private ICustomerService customerService;

    @RequestMapping("/list/{pageSize}/{pageNo}")
    @ResponseBody
    public PageInfo<MerchantUtil> list(@PathVariable int pageSize,
                                       @PathVariable int pageNo,
                                       @RequestBody MerchantVo vo){
        return merchantService.queryAll(pageNo,pageSize,vo);
    }

    @RequestMapping("/querylist/{pageNo}/{pageSize}")
    @ResponseBody
    public PageUtils<Merchant> querylist(@PathVariable int pageNo, @PathVariable int pageSize, @RequestBody MerchantVo vo){
        PageUtils<Merchant> list = merchantService.querylist(pageNo, pageSize, vo);
        return  list;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @RequestMapping("/selectById")
    @ResponseBody
    public Merchant selById(int id){
        QueryWrapper<Merchant> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        Merchant one = this.merchantService.getOne(wrapper);
        return one;
    }


    /**
     * 查询未审核的用户
     * @param pageNo
     * @param pageSize
     * @param vo
     * @return
     */
    @RequestMapping("/selAduit/{pageNo}/{pageSize}")
    @ResponseBody
    public PageUtils<Merchant> selAduit(@PathVariable int pageNo, @PathVariable int pageSize, @RequestBody MerchantVo vo){
        PageUtils<Merchant> audit = this.merchantService.audit(pageNo, pageSize, vo);
        return audit;

    }

    @ResponseBody
    @PostMapping("/insertMerchant")
    public int insertMerchant(@RequestBody Merchant merchant){
        System.out.println(merchant);
        int i = merchantService.insertMerchant(merchant);
        System.out.println(i);
        return i;
    }
    /**
     * 获取所有商户信息
     * @return 商户列表
     */
    @ResponseBody
    @GetMapping("/getAllMerchants")
    public List<Merchant> getAllMerchants() {
        return merchantService.findAllMerchants();
    }

    /**
     * 根据ID获取单个商户信息
     * @param id 商户ID
     * @return 商户对象
     */
    @ResponseBody
    @GetMapping("/getMerchantById/{id}")
    public Merchant getMerchantById(@PathVariable Integer id) {
        return merchantService.findMerchantById(id);
    }


    /**
     * 修改商户审核状态
     * @param merchant
     * @return
     */
    @RequestMapping("/updateOk")
    @ResponseBody
    public boolean updateOk(@RequestBody Merchant merchant){

        merchant.setCheckerTime(new Date());
        merchant.setUserMerchantTag(1);
        merchant.setMerchantStatus(0);
        boolean b = this.merchantService.updateById(merchant);
        if(merchant.getUserMerchantTag().equals(0)){
            QueryWrapper<Customer> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id",merchant.getId());
            Customer one = this.customerService.getOne(wrapper);
            one.setUserMechant(1);
            this.customerService.updateById(one);
        }
        return b;
    }

    @RequestMapping("/updateNot")
    @ResponseBody
    public boolean updateNot(@RequestBody Merchant merchant){
        merchant.setUserMerchantTag(3);
        boolean b = this.merchantService.updateById(merchant);
        return b;
    }
}

